package org.example.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.example.annotation.Log;
import org.example.domain.parameter.UserParameter;
import org.example.domain.po.ErpUser;
import org.example.domain.po.R;
import org.example.domain.query.UserQuery;
import org.example.domain.vo.ErpUserVO;
import org.example.enums.BusinessType;
import org.example.service.ErpRoleService;
import org.example.service.impl.ErpUserServiceImpl;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("/user")
public class ErpUserController {
    @Autowired
    private ErpUserServiceImpl erpUserService;
    
    @Resource
    private ErpRoleService roleService;

    @RequestMapping("/user")
    public R<List<ErpUser>> list() {
        return R.success(erpUserService.selectUser());
    }
    
    @GetMapping("/page")
    @ApiOperation("用户分页查询")
    public Result<IPage<ErpUserVO>> queryUserIPage(@RequestAttribute("user") ErpUser user, UserQuery userQuery){
        userQuery.setTenantId(user.getTenantId());
        IPage<ErpUserVO> userIPage = erpUserService.queryUserPage(userQuery);
        return Result.success(userIPage);
    }
    
    @Log(operation = "用户", businessType = BusinessType.INSERT, isSaveRequestData = true, paramName = "userParameter", jsonPath = "$.loginName")
    @PostMapping("/saveUser")
    @ApiOperation("新增用户")
    public Result<String> userInsert(@RequestAttribute("user") ErpUser user, @RequestBody UserParameter userParameter){
        userParameter.setTenantId(user.getTenantId());
        userParameter.setCreator(user.getId());
        userParameter.setUpdater(user.getId());
        boolean b = erpUserService.saveUser(userParameter);
        return b ?  Result.success("新增成功") : Result.error("新增失败");
    }
    
    @Log(operation = "用户", businessType = BusinessType.UPDATE, isSaveRequestData = true, paramName = "userParameter", jsonPath = "$.id")
    @PutMapping("/updateUser")
    @ApiOperation("修改用户")
    public Result<String> userUpdate(@RequestAttribute("user") ErpUser user,@RequestBody UserParameter userParameter){
        userParameter.setTenantId(user.getTenantId());
        userParameter.setCreator(user.getId());
        userParameter.setUpdater(user.getId());
        boolean b = erpUserService.updateUser(userParameter);
        return b ? Result.success("修改成功！") : Result.error("修改失败！");
    }
    
    @GetMapping("/userCount")
    @ApiOperation("根据登录名查询用户数量")
    public Result<Long> count(String loginName){
        LambdaQueryWrapper<ErpUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ErpUser::getLoginName, loginName);
        return Result.success(erpUserService.count(queryWrapper));
    }
    
    @PostMapping("/resetPass")
    @ApiOperation("重置用户密码")
    public Result<String> resetPass(@RequestBody Long userId){
        boolean b = erpUserService.resetPass(userId);
        return b ? Result.success("重置密码成功！") : Result.error("重置密码失败！");
    }
    
    @Log(operation = "用户", businessType = BusinessType.DELETE, isSaveRequestData = true, paramName = "roleLoginNames")
    @DeleteMapping("/userRemoveBatch/{userLoginNames}")
    @ApiOperation("根据登录名集合删除用户")
    public Result<String> remove(@RequestAttribute ErpUser user,  @PathVariable("userLoginNames") List<String> userLoginNames){
        return erpUserService.removeBatchByLoginNames(userLoginNames, user.getTenantId());
    }
    
    @Log(operation = "用户", businessType = BusinessType.STATUS, isSaveRequestData = true, paramName = "userIds")
    @PutMapping("/upUserStatus/{userLoginNames}/{stater}")
    @ApiOperation("修改用户状态")
    public Result updateRoleStatus(@RequestAttribute ErpUser user, @PathVariable("userLoginNames") List<String> userLoginNames,
                                   @PathVariable("stater") Integer status){
        return erpUserService.updateUserStatus(userLoginNames, user.getLoginName(), status);
    }
    
//    @GetMapping("/submitLogin")
//    @ApiOperation("用户登录")
//    public Result<String> login(@RequestAttribute("user") ErpUser user){
//        // 密码加盐
//        String pass = new BCryptPasswordEncoder().encode(user.getPassword());
//        LambdaQueryWrapper<ErpUser> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(user.getLoginName() != null, ErpUser::getLoginName, user.getLoginName())
//                        .eq(user.getPassword() != null, ErpUser::getPassword, pass);
//
//        ErpUser one = erpUserService.getOne(wrapper);
//        return Result.success("");
//    }
    
    @GetMapping("/quit")
    @ApiOperation("退出登录")
    public Result<String> quit(){
        return Result.success("");
    }
    
    @GetMapping("/findUserByTenantId")
    @ApiOperation("查询租户下的用户")
    public Result<List<ErpUser>> findUserByTenantId(@RequestAttribute("user") ErpUser user){
        return Result.success(erpUserService.findUserByTenantId(user.getTenantId()));
    }
    
    @GetMapping("/getUserCountByUserName")
    @ApiOperation("判断用户名是否存在")
    public Result<Boolean> getUserCountByUserName(ErpUser user){
        return Result.success(erpUserService.getUserCountByUserName(user));
    }
    
    @PostMapping("/register")
    @ApiOperation("注册用户")
    public Result<String> userRegister(@RequestBody ErpUser user){
        String s = erpUserService.userRegister(user);
        if (StrUtil.isNotEmpty(s)) return Result.success(s);
        return Result.error("注册失败");
    }

}
